import {
	CallHandler,
	ExecutionContext,
	Injectable,
	NestInterceptor,
} from "@nestjs/common";
import { HttpAdapterHost } from "@nestjs/core";
import { Observable, tap } from "rxjs";

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
	constructor(private adapter: HttpAdapterHost) {}

	// biome-ignore lint/suspicious/noExplicitAny: <explanation>
	intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
		const request = context.switchToHttp().getRequest();
		const url = this.adapter.httpAdapter.getRequestUrl(request);
		const method = this.adapter.httpAdapter.getRequestMethod(request);
		const label = `${method} ${url} [${context.getClass().name}.${context.getHandler().name}]`;

		console.time(label);
		return next.handle().pipe(tap(() => console.timeEnd(label)));
	}
}
